package com.mts.indicatorjpa.config

/**
 * Created on 2025/8/20 15:52.
 * @author Johnny
 */


import org.hibernate.boot.model.naming.Identifier
import org.hibernate.boot.model.naming.PhysicalNamingStrategy
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment

class CamelToSnakeNamingStrategy : PhysicalNamingStrategy {
    override fun toPhysicalCatalogName(name: Identifier?, context: JdbcEnvironment?) = name
    override fun toPhysicalSchemaName(name: Identifier?, context: JdbcEnvironment?) = name
    override fun toPhysicalTableName(name: Identifier?, context: JdbcEnvironment?) =
        convertToSnake(name)

    override fun toPhysicalSequenceName(name: Identifier?, context: JdbcEnvironment?) = name
    override fun toPhysicalColumnName(name: Identifier?, context: JdbcEnvironment?) =
        convertToSnake(name)

    private fun convertToSnake(name: Identifier?): Identifier? {
        return name?.let {
            Identifier.toIdentifier(
                it.text.replace(Regex("([a-z])([A-Z])"), "$1_$2").lowercase()
            )
        }
    }
}
